package com.baizhiedu;

import com.baizhiedu.bean.ConnectionFactoryBean;
import com.baizhiedu.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * ClassName: AppConfig
 * Description:
 * Date: 2022/12/03 PM 6:38:48
 * <p>
 * project: spring5
 * package: com.baizhiedu
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Configuration
public class AppConfig {

    /**
     * 简单对象
     *
     * @return {@link User}
     */
    @Bean("u")
    //@Scope("prototype")
    public User user() {
        return new User();
    }

    /**
     * 创建复杂对象 不能通过 new 直接创建
     *
     * @return {@link java.sql.Connection}
     */
    @Bean
    public Connection conn() {
        Connection connection;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/wgj?useSSL=false"
                    , "root", "123456");
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    @Bean
    public Connection conn1() {
        Connection connection = null;
        try {
            ConnectionFactoryBean factoryBean = new ConnectionFactoryBean();
            connection = factoryBean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return connection;
    }
}
